<%@ page import="com.pixiv.Entity.User" %>
<%--
  Created by IntelliJ IDEA.
  User: hasee
  Date: 2021/11/19
  Time: 9:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>公告发布</title>
    <link rel = "stylesheet" href = "${pageContext.request.contextPath}/common/layui/css/layui.css">
    <script src = "${pageContext.request.contextPath}/common/jquery3.6.0.js"></script>
    <script src = "${pageContext.request.contextPath}/common/layui/layui.js"></script>
</head>
<body>


<div style = "margin:0px;padding:0px;">
    <div class="demoTable">
        公告id:
        <div class="layui-inline">
            <input class="layui-input" name="NoticeID" id="NoticeID" autocomplete="off">
        </div>
        公告标题:
        <div class="layui-inline">
            <input class="layui-input" name="noticeCaption" id="noticeCaption" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload" id = "searchData">搜索</button>
        <button class="layui-btn" data-type="reload" id = "addData">加入新公告</button>
    </div>

    <!-- 内容主体区域 -->
    <table id="demo" lay-filter="test"></table>
    <div style="padding: 15px;">

    </div>
</div>

<script type = "text/javascript">

    //初始化表
    layui.use(['table'], function() {
        var table = layui.table;
        table.render
        ({
            elem: '#demo',
            url: "${pageContext.request.contextPath}/position/getNotices",
            page: {limits:[10,15,20,30,50],limit:10}, //开启分页
            cols:[[
                {title: "公告id",field:"noticeid",sort:true},
                {title: "公告标题",field:"noticeCaption",sort:true},
                {title: "公告内容",field:"noticeContext"},
                {title: "用户姓名",field:"noticeUserID",sort:true},
                {title: '操作',toolbar: '#applay'}
            ]],
            limit:2,
            limits:[2,3,5],
            request: {
                pageName: 'page',   // 页码的参数名称，默认：page
                limitName: 'size'   // 每页数据量的参数名，默认：limit
            },
            id:"showNotice"
        });

        //搜索条件
        var $ = layui.$, active = {
            reload: function(){
                var noticeid = $('#NoticeID').val();
                var noticeCaption = $('#NoticeCaption').val();
                //var userid = $('#userID').val();
                //执行重载
                table.reload('showNotice', {
                    page:{
                        curr: 1 //重新从第 1 页开始
                    },
                    where:{
                        noticeid: noticeid,
                        noticeCaption:noticeCaption
                    }
                }, 'data');
            }
        };

        //搜索按钮点击事件注册
        $('#searchData').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //加入新公告时触发的信息
        $('#addData').on('click',function(){
            var index = layer.open({
                title: '加入新公告',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['60%', '60%'],
                content: '${pageContext.request.contextPath}/back/addNotice'
            });
        });

        //点击查看公告时进行弹窗
        table.on('tool(test)', function(obj) {
            var data = obj.data;
            if (obj.event === 'show')
            {
                var index = layer.open({
                    title: '查看',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['60%', '60%'],
                    content: '${pageContext.request.contextPath}/back/showNotice?id='+data.noticeid
                });
            }else if (obj.event === 'del')
            {
                var id = data.noticeid;

                layer.confirm('确定要删除这条公告吗?', {
                    btn: ['确定','我按错了'] //按钮
                },function(){
                    $.ajax({
                        url: "${pageContext.request.contextPath}/back/deleteNotice",
                        type: "POST",
                        data: {id: id},
                        dataType:"json",
                        success:function(req)
                        {
                            if (req.flag == 1){
                                table.reload('showNotice');
                                layer.msg("删除公告成功了");

                            }
                        }
                    });
                }, function(){
                });



            }
        });



    });

</script>

<script type="text/html" id="applay">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="show">查看</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

</body>
</html>
